/**
 * Created by xiaohai on 2016/11/21.
 */
(function($) {
    $.fn.webUploadFile = function(options, uploadSuccessfun, beforeFileQueuedfun) {
        var id = $(this).attr("id");
        var fileNum = 1;
        var _multiple = true;
        if (options.fileNumLimit) {
            fileNum = options.fileNumLimit;
        }
        if (typeof(options.multiple) != "undefined") //上传单个文件
        {
            _multiple = options.multiple;
        }

        var uploader = WebUploader.create({

            // 选完文件后，是否自动上传。
            auto: true,

            fileNumLimit: fileNum,
            // swf文件路径
            swf: '/public/admin/js/webuploader/Uploader.swf',

            // 文件接收服务端。
            server: '/index/Common/uploadfile',

            // 选择文件的按钮。可选。
            // 内部根据当前运行是创建，可能是input元素，也可能是flash.
            pick: { id: "#" + id, multiple: _multiple },
            formData: options.formData,
            // 只允许选择图片文件。
            accept: options.accept,
            fileSingleSizeLimit: 20 * 1024 * 1024
        });

        uploader.on("uploadSuccess", function(file, response) {

            var pojb = $("#" + options.Ul_fileList);
            if (uploadSuccessfun) {

                uploadSuccessfun(file, response);

                if (response && response.fileid) {
                    if ($(".filedel" + response.fileid, $("#" + options.Ul_fileList)).length > 0) {
                        $(".filedel" + response.fileid).on("click", function() {
                            //FileID
                            $(".file_name" + response.fileid, pojb).remove();
                            var fids = $("#" + options.fileIDValue).val();

                            var fpath = "";
                            var fparr = [];
                            if (options.filePathValue) {
                                fpath = $("#" + options.filePathValue).val();
                                if (fpath)
                                    fpath = fpath.split(",");
                            }
                            var farr = (fids + "").split(",");


                            var nArr = returnNewArr(response.fileid, farr);

                            if (fparr.length > 0) {
                                var npArr = returnNewArr(response.savename, fparr);
                                $("#" + options.filePathValue).val(npArr.join(','));
                            }



                            $("#" + options.fileIDValue).val(fpath.join(','));
                            uploader.removeFile(file);
                            // fids returnNewArr

                        })
                    }
                }
                // if (_multiple == false) { uploader.removeFile(file); }
            }

            setTimeout(function() {
                $("#" + id + "_progress_bar").css("width", "0%");
            }, 1500);
        });
        uploader.on("beforeFileQueued", function(file) {


            if (beforeFileQueuedfun) {
                beforeFileQueuedfun(file);

            }
        });
        uploader.on("uploadProgress", function(file, percentage) {
            if ($("#" + id + "_progress_bar").length > 0)
                $("#" + id + "_progress_bar").css("width", percentage * 100 + "%");
        });

        uploader.on("error", function(handler) {

            switch (handler) {
                case "Q_EXCEED_NUM_LIMIT":

                    bootoast({
                        message: "文件最多只能上传",
                        type: 'danger',
                        position: 'right-top',
                        timeout: 2
                    });

                case "Q_TYPE_DENIED":
                    bootoast({
                        message: "文件格式错误",
                        type: 'danger',
                        position: 'right-top',
                        timeout: 2
                    });

                default:
                    bootoast({
                        message: "该文件不允许上传",
                        type: 'danger',
                        position: 'right-top',
                        timeout: 2
                    });

                    break;

            }

        });

    }


})(jQuery);



/*
 * 删除元素
 * */
function returnNewArr(value, arr) {
    var tmpArr = new Array();
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] != value) {
            tmpArr.push(arr[i]);
        }
    }
    return tmpArr;
}